<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        lazyMan = (name) => {
            var queue = []
            const task = () => {
                console.log(`你好，我是${name}`);
                next()
            }
            queue.push(task)
            const next = () => {
                const first = queue.shift()
                first ?.()
            }
            var api = {
                // _x: queue,
                sleep(n) {
                    const task = () => {
                        setTimeout(() => {
                            console.log(`我醒了，我刚睡了${n}秒`);
                            next()
                        }, n * 1000)
                    }
                    queue.push(task)
                    return api
                },
                eat(type) {
                    const task = () => {
                        console.log(type === 'lunch' ? '吃午餐' : '吃晚餐');
                        next()
                    }
                    queue.push(task)
                    return api
                },
                sleepFirst(n) {
                    const task = () => {
                        setTimeout(() => {
                            console.log(`我醒了，我刚睡了${n}秒`);
                        }, n * 1000);
                    }
                    queue.unshift(task)
                    return api
                }
            }
            setTimeout(() => {
                next()
            });
            return api
        }
        lazyMan('Hank')
        lazyMan('Hank').sleep(10).eat('lunch')
        lazyMan('Hank').eat('lunch').eat('super')
        lazyMan('Hank').sleepFirst(5).eat('super')
    </script>
</body>

</html>